<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
	Object obj = request.getSession().getAttribute("role_resource");
	if (null == obj) {
		response.setStatus(1000);
		return;
	}
%>

<script type="text/javascript">
		var Fodder = {
			uploaderror:function(msg){
				Ext.Msg.alert("Message", msg);
			},
			begin:function(){
				Ext.MessageBox.wait("上传进度","上传中，请等待。。。");
			},
			complete:function(d){
				Ext.MessageBox.updateProgress(1);
                Ext.MessageBox.hide();
				if("exists"==d){
					//Ext.Msg.alert("提示","文件已存在,请重新选择上传的文件");
					Ext.Msg.show({
 						 title:"警告",
  						 msg: "文件已存在,请重新选择上传的文件!",
  						 buttons: Ext.Msg.OK,
  						 animEl: "elId",
                        icon:Ext.MessageBox.WARNING
                        });
				}
				else if(""!=d){
					Ext.getCmp("fodder.docname").setValue(d),
					//Ext.Msg.alert("提示","上传成功!");
					Ext.Msg.show({
  							title:"提示",
  							msg:"操作成功!",
  							buttons:Ext.Msg.OK,
  							icon: Ext.MessageBox.INFO
						});
				}
			},
			validate:function(){
				var type=Ext.getCmp("fodder.type").getValue();
				//验证是否选中
				if(""==type){
					Ext.Msg.show({
 						 title:"警告",
  						 msg: "请选择素材类型!",
  						 buttons: Ext.Msg.OK,
  						 animEl: "elId",
                        icon:Ext.MessageBox.WARNING
                    });
					return false;
				}
				else if("1"==type){
					return true;
				}else{
					//Ext.Msg.alert("提示","文字类型不允许上传图片!");
					Ext.Msg.show({
 						 title:"警告",
  						 msg: "文字类型不允许上传图片!",
  						 buttons: Ext.Msg.OK,
  						 animEl: "elId",
                        icon:Ext.MessageBox.WARNING
                    });
					return false;
				}
			}
		};
		
		//素材列表数据源
		var data_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		baseParams: "",
    		fields: ["id","name","type","docname","begin","end","state","url","remark","personno","personname","createtime"],
    		url: "fodder!list.action"
    	});
    	//素材状态下拉列表
    	var state_store = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"0",text:"退回"},{id:"1",text:"启用"},{id:"2",text:"草稿"},{id:"3",text:"审核中"}]
		});
		//素材状态下拉列表
    	var state_store1 = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"",text:"全部"},{id:"0",text:"退回"},{id:"1",text:"启用"},{id:"2",text:"草稿"},{id:"3",text:"审核中"}]
		});
		//素材类别下拉列表
    	var type_store = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"1",text:"图片"},{id:"2",text:"文字"}]
		});
		//素材类别下拉列表
    	var type_store1 = new Ext.data.JsonStore({
			fields: ["id", "text"],
			data: [{id:"",text:"全部"},{id:"1",text:"图片"},{id:"2",text:"文字"}]
		});

			//定义数据显示面板
			var p1 = {
				id: "p1",
				xtype: "grid",
				stripeRows: true,
				loadMask: true,
				autoExpandColumn: "name",
				store: data_store,
				headersDisabled: true,
				sm: new Ext.grid.RowSelectionModel({
					singleSelect:true,
					listeners:{
						rowselect:function(t,rowIndex,record){
							//如果为退回状态
							if("0"==record.data.state){
								Ext.getCmp("fodder_del_but").setDisabled(true);//不可删除
								Ext.getCmp("fodder_mod_but").setDisabled(false);//可修改
								Ext.getCmp("fodder_sh_but").setDisabled(false);//可审核
							}//如果为草稿状态
							else if("2"==record.data.state){
								Ext.getCmp("fodder_del_but").setDisabled(false);//可删除
								Ext.getCmp("fodder_mod_but").setDisabled(false);//可修改
								Ext.getCmp("fodder_sh_but").setDisabled(false);//可审核
							}//如果为启用,审核状态
							else{
								Ext.getCmp("fodder_del_but").setDisabled(true);//不可删除
								Ext.getCmp("fodder_mod_but").setDisabled(true);//不可修改
								Ext.getCmp("fodder_sh_but").setDisabled(true);//不可审核
							}
						}
					}
				}),        
				columns:[
	    	    	new Ext.grid.RowNumberer({header: "序号", width: 35}),
    					{header: "编号",	dataIndex: "id", menuDisabled: true},
    					{header: "素材名称",	dataIndex: "name",id: "name", menuDisabled: true},
    					{	header: "素材类型",	
    						dataIndex: "type",
							menuDisabled: true,
    						renderer: function(input){
    							var res2=type_store.getById(input);
    							return (res2)?res2.data.text:"";
    						}
    					},
    					{header: "文件名",	dataIndex: "docname", menuDisabled: true},
    					{header: "有效期起始月",	dataIndex: "begin", menuDisabled: true},
    					{header: "有效期截止月",	dataIndex: "end", menuDisabled: true},
    					{	header: "状态",	
    						dataIndex: "state",
    						menuDisabled: true,
    						renderer: function(input){
    							var res2=state_store.getById(input);
    							return (res2)?res2.data.text:"";
    						}
    					},
    					{header: "URL",	dataIndex: "url", menuDisabled: true},
    					{header: "备注",	dataIndex: "remark", menuDisabled: true},
    					{header: "操作人",	dataIndex: "personname", menuDisabled: true},
    					{header: "创建时间",	dataIndex: "createtime",width:115, menuDisabled: true},
    					{
    						xtype:"billActionColumn",
    						header: "预览",
    						width:34,
    						dataIndex: "type",
							menuDisabled: true,
    						items:[{
			                    icon:"images/shared/icons/fam/information.png",  // Use a URL in the icon config
			                    tooltip:"预览",
			                    handler: function(grid, rowIndex, colIndex) {
									var rec = data_store.getAt(rowIndex);
									var win=Ext.getCmp("showimages");
									win.show();
									var html="<img id='imgpage' width="+win.getInnerWidth()+" height="+win.getInnerHeight()+" src='download.action?id="+encodeURI(rec.get('docname'))+"' />";
									win.update(html);
			                    }
			                }],
			                renderer: function(input){
			                	return "1"==input;
    						}
    					}
				],
				tbar: ["&nbsp&nbsp素材类型:",
				{
					xtype : "billCombox",
					width : 120,
					id : "foddertype",
					value:"",
					store : type_store1,
					listeners : {
						'select':function(){
							data_store.baseParams["fodder.type"] = this.value ;
							data_store.load({params:{start:0,limit:19}});
						}
					}
				},"&nbsp&nbsp状态:",
				{
					xtype : "billCombox",
					width : 120,
					id : "fodderstate",
					value:"",
					store : state_store1,
					listeners : {
						'select':function(val){
							data_store.baseParams["fodder.state"] = this.value ;
							data_store.load({params:{start:0,limit:19}});
						}
					}
				}],
				buttonAlign: "center",
				bbar: new Ext.ux.PagingToolbar({
    	        	pageSize: 19,
    	        	store: data_store
    	    	}),
				buttons:[{
					text:"新增", 
					handler: function(){
						oper_type = "add";
						Ext.getCmp("p2").resetParam();
						Ext.getCmp("mycontainer").layout.setActiveItem(1);
					}
				},{
					id:"fodder_mod_but",
					text:"修改", 
					handler: function(){
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if (record){
							Ext.getCmp("p2").setParam(record.data);
							oper_type = "upd";
							Ext.getCmp("mycontainer").layout.setActiveItem(1);
							Ext.getCmp("flashupload").show();
						}
						else {
							//Ext.Msg.alert("错误","没有任何行被选中，无法进行修改操作!");
							Ext.Msg.show({
		  						 title:"警告",
		   						 msg: "没有任何行被选中，无法进行修改操作!",
		   						 buttons: Ext.Msg.OK,
		   						 animEl: "elId",
		                         icon:Ext.MessageBox.WARNING
		                         });
						}			
					}
				},{
					id:"fodder_del_but",
					text:"删除", 
					handler: function(){
						oper_type="del";
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if(record){
							Ext.Msg.confirm("警告","确定要删除吗?",function(button) {
								if(button == "yes") {
									var url = "fodder!del.action";
									var params = {
										"fodder.id":record.data.id
									};
									oper(url,params);
								}
							});
						} else {
							//Ext.Msg.alert("错误","没有任何行被选中，无法进行删除操作!");
							Ext.Msg.show({
		  						 title:"警告",
		   						 msg: "没有任何行被选中，无法进行删除操作!",
		   						 buttons: Ext.Msg.OK,
		   						 animEl: "elId",
		                         icon:Ext.MessageBox.WARNING
		                         });
						}
				}},{
					id:"fodder_sh_but",
					text:"提交审核", 
					handler: function(){
						oper_type = "sh";
						var record = Ext.getCmp("p1").getSelectionModel().getSelected();
						if(record){
							url="fodder!sh.action";
							var params = {
								"fodder.id":record.data.id,
								"fodder.state":"3"
							};
							Ext.getCmp("fodder_del_but").setDisabled(true);//不可删除
							Ext.getCmp("fodder_mod_but").setDisabled(true);//不可修改
							Ext.getCmp("fodder_sh_but").setDisabled(true);//不可审核
							oper(url,params);
						} else {
							//Ext.Msg.alert("错误","没有任何行被选中，无法进行删除操作!");
							Ext.Msg.show({
		  						 title:"警告",
		   						 msg: "没有任何行被选中，无法进行提交审核操作!",
		   						 buttons: Ext.Msg.OK,
		   						 animEl: "elId",
		                         icon:Ext.MessageBox.WARNING
		                         });
						}
				}}]
			};
			
			var p2 = {
				id: "p2",
			    labelWidth: 125,
				xtype: "form",
			    bodyStyle:"padding:5px 5px 0",
			    autoWidth: true,
				defaultType: "textfield",
				items: [{
					id: "fodder.id", 
					fieldLabel: "素材编号",	
					name: "fodder.id",
					anchor: "100%"
				},{
					id: "fodder.name", 
					fieldLabel: "素材名称",		
					name: "fodder.name",
					anchor: "100%",
					validator:function (val){
		        		if(val=="")return "请输入素材名称！";
		        		if(Ext.fucCheckCHLength(val)>100){
		        			return "请输入100个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
		        		}
		        		try {
							var pat=new RegExp("[^a-zA-Z0-9\_\u4e00-\u9fa5]","i");
							if(pat.test(val))return "请输入英文字母,数字,中文，其它字符是不允许的.";
							return true;
						} catch (e) {
							return false;
						}
		        		return true;
		        	}
				},{
					id: "fodder.type", 	  
					fieldLabel: "素材类别",				
					name: "fodder.type",
					xtype: "billCombox",
					store: type_store,
					listeners:{
						select:function(combo,record,idx){
							var  cmp=Ext.getCmp("fodder.docname");
							if("1"==record.data.id){
								cmp.disable();
							}else{
								cmp.enable();
							}
						}
					}
				},{
					id: "fodder.docname", 	  
					fieldLabel: "文件名",
					disabled:true,			
					name: "fodder.docname",
					anchor: "100%",
					validator:function (val){
						var check=Ext.getCmp("fodder.type").getValue();
						if("1"==check)return true;
						else if("2"==check && val==""){
							return "请输入文本素材的内容！";
						}
		        		if(Ext.fucCheckCHLength(val)>400){
		        			return "请输入400个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
		        		}
		        		try {
							var pat=new RegExp("[^a-zA-Z0-9\_\u4e00-\u9fa5]","i");
							if(pat.test(val))return "请输入英文字母,数字,中文，其它字符是不允许的.";
							return true;
						} catch (e) {
							return false;
						}
		        	}
				},{
					id: "fodder.begin", 	  
					fieldLabel: "有效期起始月",				
					name: "fodder.begin",
					xtype: "datefield",
					format: "Y-m",
					editable: false,
					anchor: "100%",
					validator:function(val){
						var edate=Ext.getCmp("fodder.end").getValue();
						if(""==edate)return true;
						if(edate<this.getValue()){
							return "开始日期不能早于结束日期";
						}
						return true;
					}
				},{
					id: "fodder.end", 	  
					fieldLabel: "有效期截止月",				
					name: "fodder.end",
					xtype: "datefield",
					format: "Y-m",
					editable: false,
					anchor: "100%",
					validator:function(val){
						var bdate=Ext.getCmp("fodder.begin").getValue();
						if(""==bdate)return true;
						if(bdate>this.getValue()){
							return "开始日期不能早于结束日期";
						}
						return true;
					}
				},{
					id: "fodder.state", 	  
					fieldLabel: "状态",				
					name: "fodder.state",
					xtype: "billCombox",
					disabled:true,
					store: state_store
				},{
					id: "fodder.url", 	  
					fieldLabel: "URL",				
					name: "fodder.url",
					vtype:"url",
					anchor: "100%"
				},{
					id: "fodder.remark", 	  
					fieldLabel: "备注",				
					vtype:"alphanumchinese",
					name: "fodder.remark",
					anchor: "100%",
					validator:function (val){
		        		if(val=="")return true;
		        		if(Ext.fucCheckCHLength(val)>200){
		        			return "请输入200个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
		        		}
		        		return true;
		        	}
				}],
				buttonAlign: "center",
				buttons:[{text:"保存",id:"save",  handler: function(e){
					var form = this.ownerCt.ownerCt.getForm();
					if(!form.isValid()){
						Ext.Msg.show({
							title:"警告",
							msg: "验证不通过!",
							buttons: Ext.Msg.OK,
							animEl: "elId",
							icon:Ext.MessageBox.WARNING
						});
						return;
					}
					var url,params;
					if(oper_type == "add") {
						url="fodder!add.action";
					} else if(oper_type == "upd") {
						url="fodder!upd.action";
					}
					params = {
						"fodder.id": Ext.getCmp("fodder.id").getValue(),
						"fodder.name": Ext.getCmp("fodder.name").getValue(),
						"fodder.type": Ext.getCmp("fodder.type").getValue(),
						"fodder.docname": Ext.getCmp("fodder.docname").getValue(),
						"fodder.begin": Ext.getCmp("fodder.begin").getValue(),
						"fodder.end": Ext.getCmp("fodder.end").getValue(),
						"fodder.state": Ext.getCmp("fodder.state").getValue(),
						"fodder.url": Ext.getCmp("fodder.url").getValue(),
						"fodder.remark": Ext.getCmp("fodder.remark").getValue()
					};					
					e.setDisabled(true);
					oper(url,params);
				}},{text:"取消", handler: function(){
					this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
				}},{
		           	 xtype:"flash"
		           	 ,id:"flashupload"
		           	 ,anchor:"anchor"
		           	 ,expressInstall:true//如果没有安装flash，提示用户进行安装
		           	 ,width:70
		           	 ,height:22
		           	 ,url:"flash/upload.swf"
		           	 ,flashParams:{
		           	 	quality:"high",
		           	 	allowScriptAccess:"sameDomain",
		           	 	allowFullScreen:true
		           	 }
		           	 ,flashVars:{
						requestUrl:"upload.action",
						type:"fodder",
						fileFilterName:"Images (*.jpg, *.jpeg, *.png ,*.gif)",
						fileFilterValue:"*.jpg;*.jpeg;*.png;*.gif",
						error:encodeURI(Fodder.uploaderror),
						complete:encodeURI(Fodder.complete),
						begin:encodeURI(Fodder.begin),
						validate:encodeURI(Fodder.validate)
					  }
			}],
			setParam: function(data){
				Ext.getCmp("fodder.id").setValue(data.id);
				Ext.getCmp("fodder.id").setDisabled(true);
				Ext.getCmp("fodder.id").setVisible(true);
				Ext.getCmp("fodder.name").setValue(data.name);
				Ext.getCmp("fodder.type").setValue(data.type);
				Ext.getCmp("fodder.docname").setValue(data.docname);
				Ext.getCmp("fodder.begin").setValue(data.begin);
				Ext.getCmp("fodder.end").setValue(data.end);
				Ext.getCmp("fodder.state").setValue(data.state);
				Ext.getCmp("fodder.url").setValue(data.url);
				Ext.getCmp("fodder.remark").setValue(data.remark);
			},
			resetParam: function(){
				Ext.getCmp("fodder.id").setVisible(false);
				Ext.getCmp("fodder.name").setValue("");
				Ext.getCmp("fodder.type").setValue("");
				Ext.getCmp("fodder.docname").setValue("");
				Ext.getCmp("fodder.begin").setValue("");
				Ext.getCmp("fodder.end").setValue("");
				Ext.getCmp("fodder.state").setValue("2");
				Ext.getCmp("fodder.url").setValue("");
				Ext.getCmp("fodder.remark").setValue("");
			}
		};
		var fodder_pel = new Ext.Panel({
				id: "mycontainer",
				border : false,	
				layout: "card",
				activeItem: 0,
				items: [p1, p2]
			});	
		data_store.load({params:{start:0,limit:19}});
		
		var oper = function(url, params){
			Ext.Ajax.request({
				clientValidation: true,
				params: params,
				url: url,
				success: function(response, opts){
					var d=eval("("+response.responseText+")");
					if(d.code == "1"){
						if("del"==oper_type){
							Ext.Msg.show({
							   title:"提示",
							   msg:"删除成功!",
							   buttons: Ext.Msg.OK,
							   icon: Ext.MessageBox.INFO
							});
						}else{
							//Ext.Msg.alert("提交成功",d.data);
							Ext.Msg.show({
		   						title:"提示",
		   						msg:"操作成功!",
		   						buttons:Ext.Msg.OK,
		   						icon: Ext.MessageBox.INFO
							});
						}
						Ext.getCmp("p1").getBottomToolbar().doRefresh();
						Ext.getCmp("mycontainer").layout.setActiveItem(0);
					} else {
						//Ext.Msg.alert("失败",d.data);
						Ext.Msg.show({
		   					title:"提示",
		   					msg:"操作失败!",
		   					buttons:Ext.Msg.OK,
		   					icon: Ext.MessageBox.ERROR
							});
					}
					
					Ext.getCmp("save").setDisabled(false);
				}
			});
		};
		
		var CenterPanel=Ext.getCmp("CenterPanel");
		CenterPanel.removeAll();
		CenterPanel.add(fodder_pel);
		CenterPanel.doLayout();
	</script>
